<template>
  <div class="users">
    <h1>用户列表</h1>
    <div style="margin-top: 15px; display: flex">
      <el-input
        placeholder="请输入内容"
        v-model="keywords"
        class="input-with-select"
      >
       <el-button slot="append" icon="el-icon-search" @click="searchVal"></el-button>
      </el-input>
      <el-button type="primary" icon="el-icon-search" @click="openModal">添加用户</el-button>
    </div>
    <!-- table表格 -->
    <el-table :data="userList" border style="width: 100%">
     
      <el-table-column prop="username" label="姓名" > </el-table-column>
      <el-table-column prop="email" label="邮箱">
      </el-table-column>
      <el-table-column prop="mobile" label="电话"> </el-table-column>
      <el-table-column prop="role_name" label="角色">
      </el-table-column>
      <el-table-column prop="mg_state" label="开关" >
           <template slot-scope="scope">
            <el-switch
                v-model="scope.row.mg_state"
                active-color="#13ce66"
                @change="handleChange(scope.row)"
                inactive-color="#ff4949">
             </el-switch>
           </template>
         </el-table-column>
          <el-table-column prop="create_time" label="创建时间" >
            <template slot-scope="scope">
                 {{scope.row.create_time | filTime}}
            </template>
         </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row)" type="text" size="small"
            >编辑</el-button
          >
          <el-button type="text" size="small" @click="handleDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
     <el-pagination
      @size-change="handleCurrentChange"
      @current-change="handleCurrentChange"
      :current-page.sync="queryInfo.pagenum"
      :page-sizes="[2, 3, 4, 5]"
      :page-size.sync="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <!-- 添加用户模态框 -->
    <el-dialog
        :title="form.id?'编辑':'添加'"
        :visible.sync="dialogVisible"
        width="30%"
        >
       <el-form ref="form" :model="form" label-width="80px">
            <el-form-item label="用户名">
                <el-input v-model="form.username" :disabled="Boolean(form.id)"></el-input>
            </el-form-item>
            <el-form-item label="密码" v-if="!form.id">
                <el-input v-model="form.password"></el-input>
            </el-form-item>
            <el-form-item label="电话">
                <el-input v-model="form.mobile"></el-input>
            </el-form-item>
            <el-form-item label="邮箱">
                <el-input v-model="form.email"></el-input>
            </el-form-item>
       </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="submitUser">确 定</el-button>
        </span>
        </el-dialog>
  </div>
</template>
<script>
import { getUserApi,updateStatusAPi,deleteUserApi,addUserApi,editUserApi } from "@/utils/api.js";
import dayjs from 'dayjs'
export default {
  data() {
    return {
      keywords: "", //搜索框内容
      queryInfo: {
        query: "", //查询参数
        pagenum: 1, //当前页
        pagesize: 2, //每页条数
      },
      userList: [], //存放用户列表数据
      dialogVisible:false,
      form:{
        username:'',
        password:'',
        mobile:'',
        email:''
      },
      total:0
    };
  },
  created() {
    this.getUser();
  },
  methods: {
    async getUser() {
      let res = await getUserApi(this.queryInfo);
      console.log(res);
      this.userList = res.users;
      this.total = res.total
    },
    // 查询
    searchVal(){
        // 把关键字复制给查询的参数
        this.queryInfo.query = this.keywords
        this.getUser()
    },
    // 修改状态
    async handleChange(row){
        console.log(row);
       await updateStatusAPi(row)
        this.getUser()
    },
    // 删除
    handleDel(id){
    //  友情提示
     this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
        //   点击确定按钮
            await deleteUserApi({id:id})
            this.getUser()

        }).catch(() => {
            // 点击取消按钮
              
        });
    },
    // 添加/编辑用户
   async submitUser(){
        if(this.form.id){
            // 编辑用户
        const {email,mobile} = this.form
          await  editUserApi(this.form.id,{email,mobile})
          this.getUser()
        }else{
            // 添加用户
          await addUserApi(this.form)
           this.getUser()
        }
        this.dialogVisible = false
    },
    openModal(){
        // 清楚form里面id
      this.form.id = ''
      this.form = {}
      this.dialogVisible = true
    },
    // 点击编辑
    handleEdit(row){
       this.dialogVisible = true
    //    回显
    this.form = row
    },
    handleCurrentChange(){
        // console.log(this.queryInfo.pagenum);
        // console.log(this.queryInfo.pagesize);
        // 使用sync  每页条数  或者当前发生变化 在这个里面都可以及时获取到  
        this.getUser()
    }

  },
  filters:{
     filTime(val){
        return dayjs(val*1000).format('YYYY:MM:DD')

     }
  }
};
</script>
